/**
 * 文件名称 : KeyboardHandler.java
 * <p>
 * 作者信息 : maidou
 * <p>
 * 创建时间 : 2011-8-4, 上午7:27:16
 * <p>
 * 版权声明 : Copyright (c) 2009-2012 CIeNET Ltd. All rights reserved
 * <p>
 * 评审记录 :
 * <p>
 */

package com.ttomato.game.framework.impl;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.View.OnKeyListener;

import com.ttomato.game.framework.Input.KeyEvent;
import com.ttomato.game.framework.Pool;
import com.ttomato.game.framework.Pool.PoolObjectFactory;

/**
 * 请在这里增加文件描述
 * <p>
 */
public class KeyboardHandler implements OnKeyListener
{
    
    boolean[] mPressedKeys = new boolean[128];
    
    Pool<KeyEvent> mKeyEventPool;
    
    List<KeyEvent> mKeyEventsBuffer = new ArrayList<KeyEvent>();
    
    List<KeyEvent> mKeyEvents = new ArrayList<KeyEvent>();
    
    public KeyboardHandler(View view)
    {
        PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>()
        {
            
            
            public KeyEvent createObject()
            {
                // TODO Auto-generated method stub
                return new KeyEvent();
            }
        };
        
        mKeyEventPool = new Pool<KeyEvent>(factory, 100);
        
        view.setOnKeyListener(this);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
    }
    
    
    public boolean onKey(View v, int keyCode, android.view.KeyEvent event)
    {
        // TODO Auto-generated method stub
        if (event.getAction() == android.view.KeyEvent.ACTION_MULTIPLE)
        {
            return false;
        }
        
        synchronized (this)
        {
            KeyEvent keyEvent = mKeyEventPool.newObject();
            keyEvent.keyCode = keyCode;
            keyEvent.keyChar = (char) event.getUnicodeChar();
            
            if (event.getAction() == android.view.KeyEvent.ACTION_DOWN)
            {
                keyEvent.type = KeyEvent.KEY_DOWN;
                if (keyCode > 0 && keyCode < 127)
                {
                    mPressedKeys[keyCode] = true;
                }
            }
            
            if (event.getAction() == android.view.KeyEvent.ACTION_UP)
            {
                keyEvent.type = KeyEvent.KEY_UP;
                if (keyCode > 0 && keyCode < 127)
                {
                    mPressedKeys[keyCode] = false;
                }
            }
            
            mKeyEventsBuffer.add(keyEvent);
        }
        
        return false;
    }
    
    public boolean isKeyPressed(int keyCode)
    {
        if (keyCode < 0 || keyCode > 127)
        {
            return false;
        }
        return mPressedKeys[keyCode];
    }
    
    public List<KeyEvent> getKeyEvents()
    {
        synchronized (this)
        {
            int len = mKeyEvents.size();
            for (int i = 0; i < len; i++)
            {
                mKeyEventPool.free(mKeyEvents.get(i));
            }
            
            mKeyEvents.clear();
            mKeyEvents.addAll(mKeyEventsBuffer);
            mKeyEventsBuffer.clear();
            
            return mKeyEvents;
        }
    }
}
